<template>
    <page-view>
        <el-card>
            <template #header>
                <div class="font-bold">地址信息列表</div>
            </template>
            <el-form :model="queryFormData" :inline="true">
                <el-form-item label="昵称">
                    <el-input placeholder="请输入昵称" v-model="queryFormData.nickName"></el-input>
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input placeholder="请输入手机号" v-model="queryFormData.phone"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="queryData" :loading="isLoading" :loading-icon="Eleme">
                        <el-icon>
                            <Search />
                        </el-icon>
                        查询
                    </el-button>
                </el-form-item>
            </el-form>
            <div v-loading="isLoading">
                <el-table :data="resultData.listData" border stripe max-height="550">
                    <el-table-column align="center" width="100" label="ID" prop="id"></el-table-column>
                    <el-table-column width="210" label="所属用户" prop="userInfo.nickName"></el-table-column>
                    <el-table-column width="180" align="center" label="手机号" prop="phone"></el-table-column>
                    <el-table-column align="center" label="地址" prop="address"></el-table-column>
                    <el-table-column width="240" label="联系人" prop="person_name"></el-table-column>
                    <el-table-column align="center" width="130" label="标签" prop="tag"></el-table-column>
                    <el-table-column label="操作" width="130" align="center">
                        <template #default="{ row }">
                            <el-popconfirm :icon="InfoFilled" icon-color="orange" title="确定要删除吗?"
                                @confirm="delCurrentItem(row.id)">
                                <template #reference>
                                    <el-button :plain="true" type="danger" size="small" @click="visible = true">
                                        <el-icon>
                                            <Delete />
                                        </el-icon>
                                        删除
                                    </el-button>
                                </template>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- 页码 -->
                <div class="flex flex-row justify-between items-center mt-[10px]">
                    <div class="text-[14px]">当前第{{ queryFormData.pageIndex }}页，共{{ resultData.pageCount }}页，共{{
                            resultData.totalCount
                    }}条</div>
                    <el-pagination background layout="prev, pager, next" :total="resultData.totalCount"
                        @current-change="currentChange" />
                </div>
            </div>
        </el-card>
    </page-view>
</template>

<script setup>
import { Search, Eleme, InfoFilled, Delete } from "@element-plus/icons-vue"
import { ref, reactive, onMounted } from "vue"
import API from "../../utils/API";
import { ElMessage } from 'element-plus'
const visible = ref(false)
const queryFormData = reactive({
    nickName: "",
    phone: "",
    pageIndex: 1
})

const resultData = reactive({
    listData: [],
    pageCount: 0,
    pageStart: 0,
    totalCount: 0
})

const queryData = () => {
    isLoading.value = true;
    API.addressInfo.getListByPage(queryFormData)
        .then(result => {
            // console.log(result);
            resultData.listData = result.listData;
            resultData.pageCount = result.pageCount;
            resultData.pageStart = result.pageStart;
            resultData.totalCount = result.totalCount;
        }).finally(() => {
            isLoading.value = false;
        })
}
const isLoading = ref(false);


const currentChange = (index) => {
    queryFormData.pageIndex = index;
    queryData();
}



// 删除功能
const delCurrentItem = (id) => {
    // console.log(id);
    API.addressInfo.deleteById(id)
        .then(() => {
            ElMessage.success('删除成功')
            queryData();
        }).catch(() => {
            ElMessage.error('删除失败')
        })
}
const cancelEvent = () => {
    console.log("");
}

onMounted(() => {
    queryData();
})
</script>


<style scoped lang="scss">

</style>